package com.study.happystudy.controller;

import com.study.happystudy.entity.ResultEntity;
import com.study.happystudy.mapper.MessageMapper;
import com.study.happystudy.po.Post;
import com.study.happystudy.po.PostRequest;
import com.study.happystudy.po.Reply;
import com.study.happystudy.po.User;
import com.study.happystudy.service.impl.PostsServiceImpl;
import com.study.happystudy.service.impl.UserServiceImpl;
import com.study.happystudy.tool.ColorChange;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("post/")
public class PostController {

    @Resource
    PostsServiceImpl p;
    @Resource
    UserServiceImpl u;
    @Resource
    MessageMapper m;


    // 发布帖子
    @PostMapping("posting")
    public String addPost(@RequestBody PostRequest postRequest) {

        try {
            if (u.findUserById(postRequest.getAuthorId()) == null) {
                return ResultEntity.Fail(-1, "用户不存在");
            }

            p.addPost(postRequest.getAuthorId(), List.of(postRequest.getTags()), postRequest.getTitle(),
                    postRequest.getContent(), List.of(postRequest.getImages()));
        } catch (Exception e) {
            return ResultEntity.Fail(-1, "失败: \n" + e.getMessage());
        }

        return ResultEntity.Success(1, "成功");
    }


    // 根据id返回帖子
    @GetMapping("getPostById")
    public Object getPostById(@RequestParam("postId") int postId) {
        return p.getPostById(postId);
    }

    // 删除帖子
    @DeleteMapping("deletePostById")
    public String deletePostById(@RequestParam("postId") int postId) {
        if (p.getPostById(postId) == null) return ResultEntity.Fail(-1, "帖子不存在");
        try {
            p.deletePostById(postId);
        } catch (Exception e) {
            ColorChange.Red("Wrong: " + e.getMessage());
            return ResultEntity.Fail(-1, "失败: \n" + e.getMessage());
        }
        return ResultEntity.Success(1, "哟西");
    }

    // 根据用户id返回其所有帖子
    @GetMapping("getPostsByUserId")
    public List<Post> getPostsByUserId(@RequestParam("userId") int userId) {
        return p.getPostsByUserId(userId);
    }

    // 根据帖子id返回帖子的所有回复
    @GetMapping("getRepliesByPostId")
    public List<Reply> getRepliesByReplyId(@RequestParam("postId") int postId) {
        return p.getRepliesByPostId(postId);
    }

    // 根据帖子标题、内容、用户昵称、用户账户号模糊匹配帖子
    @GetMapping("searchPosts")
    public List<Post> searchPosts(@RequestParam("keyword") String keyword) {
        return p.searchPosts(keyword);
    }

    // 用户对帖子点赞
    // 返回用户和帖子
    @PostMapping("likePost")
    public String likePost(@RequestParam("userId") int userId,
                           @RequestParam("postId") int postId) {

        if (p.getPostById(postId) == null) {
            return ResultEntity.Fail(-1, "帖子不存在");
        }
        if (u.findUserById(userId) == null) {
            return ResultEntity.Fail(-1, "用户不存在");
        }

        boolean isUserLikePost = p.isUserLikePost(userId, postId);

        int rId = p.getPostById(postId).getAuthorId();
        User sender = u.findUserById(userId);
        String senderName = sender.getUserName();

        if (isUserLikePost == false) {// 用户没有点赞
            p.likePost(userId, postId);
            // 发送社区消息
            m.sendCommunityMessage(userId, rId, senderName + "赞了你的帖子");
            return ResultEntity.Success(1, "点赞成功");
        }

        //用户已点赞，取消点赞
        try {
            p.unlikePost(userId, postId);
        } catch (Exception e) {
            ColorChange.Red("Wrong" + e.getMessage());
        }

        return ResultEntity.Success(1, "取消点赞成功");
    }

    @GetMapping("isUserLikePost")
    boolean isUserLikePost(@RequestParam("userId") int userId, @RequestParam("postId") int postId) {
        return p.isUserLikePost(userId, postId);
    }

    // 用户对帖子回复
    @PostMapping("replyPost")
    public String addReply(@RequestParam("postId") int postId,
                           @RequestParam("replierId") int replierId,
                           @RequestParam("replyContent") String replyContent) {


        if (p.getPostById(postId) == null) {
            return ResultEntity.Fail(-1, "帖子不存在");
        }
        if (u.findUserById(replierId) == null) {
            return ResultEntity.Fail(-1, "用户不存在");
        }

        p.addReply(postId, replierId, replyContent);

        // 发送社区消息
        m.sendCommunityMessage(replierId, p.getPostById(postId).getAuthorId(),
                u.findUserById(replierId).getUserNickname() + "回复了你的帖子:" + replyContent);

        return ResultEntity.Success(1, "回复成功");
    }

    // 用户对帖子回复点赞
    @PostMapping("likeReply")
    public String likeReply(@RequestParam("userId") int userId,
                            @RequestParam("replyId") int replyId) {

        if (p.getRepliesByPostId(replyId) == null) {
            return ResultEntity.Fail(-1, "帖子不存在");
        }
        if (u.findUserById(userId) == null) {
            return ResultEntity.Fail(-1, "用户不存在");
        }

        boolean isUserLikeReply = p.isUserLikeReply(userId, replyId);

        Reply re = p.getReplyById(replyId);

        if (isUserLikeReply == false) {
            p.likeReply(userId, replyId);
            m.sendCommunityMessage(userId, re.getReplierId(), "赞了你的回复");
        } else {
            p.unlikeReply(userId, replyId);
            return ResultEntity.Success(1, "取消点赞成功");
        }

        return ResultEntity.Success(1, "点赞成功");
    }

    @GetMapping("isUserLikeReply")
    public boolean isUserLikeReply(@RequestParam("userId") int userId, @RequestParam("replyId") int replyId) {
        return p.isUserLikeReply(userId, replyId);
    }

    @DeleteMapping("deleteReplyById")
    public String deleteReplyById(@RequestParam("replyId") int replyId) {

        if(p.getRepliesByPostId(replyId) == null){
            return ResultEntity.Fail(-1, "回复不存在");
        }

        try {
            p.deleteReplyById(replyId);
        } catch (Exception e) {
            ColorChange.Red("Wrong: " + e.getMessage());
            return ResultEntity.Fail(-1, "失败: \n" + e.getMessage());
        }

        return ResultEntity.Success(1, "删除成功");
    }
    @GetMapping("getAllPosts")
    public Object getAllPosts() {
        try{
            return p.getAllPosts();
        }catch (Exception e){
            ColorChange.Red("Wrong: " + e.getMessage());
            return ResultEntity.Fail(-1, "Fail because Pooh: \n" + e.getMessage());
        }
    }
}
